
# -*- coding: UTF-8 -*-
import datetime
import os, sys
CUR_PATH = os.path.dirname(os.path.abspath(__file__))
PKG_PATH = os.path.abspath(os.path.dirname(CUR_PATH))

sys.path.append(CUR_PATH)
sys.path.append(PKG_PATH)


from db_interface import db_interface
import requests
import random
import json
import time
from cal_func.news_monitor import \
    get_sina_news_data, sina_news_spider

from cal_func.ids_change_interval import get_ids_change



def recent_data_news_content():
    # bigdata4——job
    news_list = get_sina_news_data(days_delta=31)[1]
    for id in news_list:
        date, link = news_list[id]
        # print(id, date, link)
        cont = sina_news_spider(link)
        para_dict = {
            'news_id': id,
            'date': date,
            'link': link,
        }
        nlp_extract = []
        for item in cont:
            item = item.strip()
            if item and len(item) >= 10:
                r = requests.get(url='http://47.92.245.140/api/v1/news_clf?content='+item)
                print(id, date, r.text)
                clf = json.loads(r.text)['data']
                info = clf['infos']
                if len(info) >= 2:
                    nlp_extract.append(
                        clf
                    )
        para_dict['extract'] = json.dumps(nlp_extract, ensure_ascii=False)
        print(para_dict)
        # if len(nlp_extract) > 0:
        db_interface.stock_base.insert_dict('news_clf', para_dict)
        time.sleep(0.001*random.choice(range(100)))




def ids_has_sotck():
    sql = "select ids, code, name, mkv from stock_info order by ids, mkv desc "
    query = db_interface.stock_base.select(sql)
    data_dict = {}
    stock_list = []
    i = 0
    for item in query:
        ids, code, name, mkv = item
        if ids not in data_dict:
            data_dict[ids] = []
            i = 0
        if i < 3:
            data_dict[ids].append(code)
            stock_list.append(code)
            i += 1

    return data_dict, stock_list



def stock_finance_interval(days=210 +365):
    today = datetime.datetime.today()
    prev_day = today - datetime.timedelta(days=days)

    prev_day = prev_day.strftime('%Y-01-01')
    today = today.strftime("%Y-%m-%d")

    return prev_day, today



def cal_stock_finance(code_data = ('000001', '000002')):
    date_interval = stock_finance_interval()
    sql = f"select distinct a.date, a.code, b.name, a.jzc_syl from " \
          f"(select distinct date, code,  jzc_syl from basic_finance where `type` = 'jd' and code in {tuple(code_data)} " \
          f"and date between '{date_interval[0]}' and '{date_interval[1]}') a " \
          f"inner join " \
          f"(select code, name from stock_info) b " \
          f"on a.code = b.code " \
          f"order by a.code, a.date desc"
    # sql = f"select distinct date, code,  jzc_syl from basic_finance " \
    #       f"where `type` = 'jd' and code in {tuple(code_data)} " \
    #       f"and date between '{date_interval[0]}' and '{date_interval[1]}' order by code, date desc"
    query = db_interface.stock_base.select(sql)
    data_dict = {}
    for item in query:
        date, code, name, jzc = item
        date = date.strftime('%Y-%m-%d')
        if code not in data_dict:
            data_dict[code] = []
        data_dict[code].append((date, jzc, name))

    res = {}
    for k, v in data_dict.items():
        nv = []
        for i in range(len(v)-1):
            d = v[i][0]
            val = v[i][1]
            if '03-31' not in v[i][0]:
                val = round(v[i][1] - v[i+1][1], 2)
            nv.append((d, val))

        jzc_avg = round(sum([x[1] for x in nv])/6, 3)
        res[k] = {
            'avg': jzc_avg,
            'name': v[0][2],
            'detail': nv[:6]
        }

    return res




def ids_stocks_avg_data():
    ids, stock_list = ids_has_sotck()
    stock_finance = cal_stock_finance(stock_list)
    # print(stock_finance)
    result = []
    ids_change = get_ids_change()
    for k, v in ids.items():
        syl_sum = []
        detail = []
        for val in v:
            if val in stock_finance:
                finance = stock_finance[val]
                syl_sum.append(finance['avg'])
                detail.append({
                    'code': val,
                    'name': finance['name'],
                    'finance': finance['avg'],
                    'season': finance['detail']
                })
        syl_avg = round(sum(syl_sum)/len(syl_sum), 3)
        change, rank = 0, 0
        if k in ids_change:
            change, rank = ids_change[k]
        result.append({
            'ids': k,
            'avg': syl_avg,
            'change': change,
            'rank': rank,
            'detail': detail
        })


    result = sorted(result, key=lambda x: x['avg'], reverse=True)
    for index, item in enumerate(result):
        print(index, item)





if __name__ == "__main__":
    r = ids_stocks_avg_data()
    print(r)

